<template>
  <div class="content">
    <div class="mainContent">
      <openProject
        ref="openProject"
        :loading="loading"
        :open-project-btn="true"
        :project-data="currentProjectData"
        @changeCurrentTag="changeCurrentTag"
        @handleOpen="handleOpen"
        @initCurrentProject="initCurrentProject"
      />
    </div>
    <projectDetail ref="projectDetail" @refreshData="handleOpen" />
    <tagsView
      ref="tagsView"
      :show-project-list="false"
      :tags-data="openProjectData"
      @changePageType="changePageType"
      @saveProjectSetting="saveProjectSetting"
    />
  </div>
</template>

<script>
import { initConfig } from '@/api/project-management'
import tagsView from './components/tagsView'
import openProject from './components/openProject.vue'
import projectDetail from './components/projectDetail.vue'
import defaultSettings from '@/settings'
import { generateUUID, postMessage, receivedMessage } from '@/utils/index'
export default {
  name: 'ProjectSetting',
  components: { tagsView, openProject, projectDetail },
  beforeRouteEnter(to, from, next) {
    next((vm) => {
      vm.getData()
    })
  },
  data() {
    return {
      loading: false,
      openProjectData: [],
      currentProjectData: {},
      initConfigRequestId: generateUUID(),
      initNewConfigRequestId: generateUUID(),
      currentProject: {}
    }
  },
  created() {},
  mounted() {
    const query = this.$route.query
    if (query.projectId) {
      this.loading = true
      this.handleOpen('add', { projectId: Number(query.projectId) })
    }
    receivedMessage(this.dataHandle)
  },
  methods: {
    dataHandle(result) {
      let self = this
      let e = defaultSettings.isWPF ? result : { data: JSON.parse(result) }
      if (e.data.webInfo.requestId === self.initConfigRequestId) {
        self.currentProject.layoutContent = e.data.response.data.layoutContent
        self.$refs.openProject.$refs.chipFlowChart.initData(self.currentProject.layoutContent)
      } else if (e.data.webInfo.requestId === self.initNewConfigRequestId) {
        self.initProjectConfig(e.data.response)
      }
    },
    getData() {
      this.$refs.openProject.initData()
    },
    initCurrentProject(projectId) {
      this.currentProject = this.openProjectData.find((item) => item.projectId === projectId)
      if (defaultSettings.isWPF || defaultSettings.isCEF) {
        this.initConfigRequestId = generateUUID()
        let message = {
          RequestUrl: process.env.VUE_APP_URL + '/project/initConfig',
          RequestMethod: 'POST',
          Headers: {
            contentType: 'application/json'
          },
          QueryParameter: {},
          RequestBody: { projectId: projectId },
          WebInfo: {
            requestId: this.initConfigRequestId
          }
        }
        postMessage(message)
      } else {
        initConfig({ projectId: projectId }).then((res) => {
          this.currentProject.layoutContent = res.data.layoutContent
          this.$refs.openProject.$refs.chipFlowChart.initData(this.currentProject.layoutContent)
        })
      }
    },
    changeCurrentTag(isSaved) {
      this.$refs.tagsView.changeSaved(isSaved)
    },
    changePageType(val, oldValue) {
      if (oldValue) {
        let history = this.openProjectData.find((item) => item.projectId === oldValue)
        if (history) {
          history.layoutContent = this.$refs.openProject.$refs.chipFlowChart.getChartData()
        }
      }
      if (val) {
        this.currentProjectData = this.openProjectData.find((item) => item.projectId === val)
        this.$nextTick(() => {
          this.$refs.openProject.$refs.chipFlowChart.initData(this.currentProjectData.layoutContent)
        })
      } else {
        this.currentProjectData = {}
      }
    },
    handleOpen(type, data) {
      let isOpen = this.openProjectData.findIndex((item) => item.projectId === data.projectId)
      if (isOpen === -1) {
        this.currentProject = data
        if (defaultSettings.isWPF || defaultSettings.isCEF) {
          this.initNewConfigRequestId = generateUUID()
          let message = {
            RequestUrl: process.env.VUE_APP_URL + '/project/initConfig',
            RequestMethod: 'POST',
            Headers: {
              contentType: 'application/json'
            },
            QueryParameter: {},
            RequestBody: { projectId: data.projectId },
            WebInfo: {
              requestId: this.initNewConfigRequestId
            }
          }
          postMessage(message)
        } else {
          initConfig({ projectId: data.projectId }).then((res) => {
            this.initProjectConfig(res)
          })
        }
      } else {
        this.$refs.tagsView.handleSelectedTag(data)
      }
    },
    initProjectConfig(res) {
      this.loading = false
      if (res.success) {
        this.openProjectData.push({
          ...res.data,
          saved: true
        })
        this.$refs.tagsView.handleSelectedTag(this.currentProject)
      } else {
        this.$message.error(res.message)
      }
    },
    saveProjectSetting() {
      this.$refs.openProject.handleClick('save')
    }
  }
}
</script>

<style lang="less" scoped>
.content {
  height: 100%;
  display: flex;
  flex-direction: column;
  .mainContent {
    flex: 1;
    height: calc(100% - 36px);
  }
}
</style>
